home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload Trio 2 / Shareware Overload Trio Volume 2 (Chestnut CD-ROM).ISO / dir30 / heaven_1.zip / DDSEARCH.LSP < prev    next >
Lisp/Scheme  |  1993-08-23  |  9KB  |  281 lines

  1. ;this is a AutoLISP routine for implementing a search and replace
  2. ;command in AutoCAD Release 12. It takes advantage of the dialogs
  3. ;in Release 12 to make it easier to use. This is a good replacement
  4. ;for the old CHGTEXT command.
  5. ;written by:
  6. ;Michael Jenkins
  7. ;Gray Construction Co.
  8. ;Lexington, Kentucky
  9.  
  10. ;;; ===================== load-time error checking ============================
  11. ;;;
  12.  
  13. (defun ai_abort (app msg)
  14.    (defun *error* (s)
  15.       (if old_error (setq *error* old_error))
  16.       (princ)
  17.    )
  18.    (if msg
  19.       (alert (strcat " Application error: "
  20.             app
  21.             " \n\n  "
  22.             msg
  23.             "  \n"
  24.          )
  25.       )
  26.    )
  27.    (exit)
  28. )
  29.  
  30. ;;; Check to see if AI_UTILS is loaded, If not, try to find it,
  31. ;;; and then try to load it.
  32. ;;;
  33. ;;; If it can't be found or it can't be loaded, then abort the
  34. ;;; loading of this file immediately, preserving the (autoload)
  35. ;;; stub function.
  36.  
  37. (cond
  38.    (  (and ai_dcl (listp ai_dcl)))          ; it's already loaded.
  39.  
  40.    (  (not (findfile "ai_utils.lsp"))                     ; find it
  41.       (ai_abort "DDCHPROP"
  42.          (strcat "Can't locate file AI_UTILS.LSP."
  43.    "\n Check support directory.")))
  44.  
  45.    (  (eq "failed" (load "ai_utils" "failed"))            ; load it
  46.    (ai_abort "DDCHPROP" "Can't load file AI_UTILS.LSP"))
  47. )
  48.  
  49. (if (not (ai_acadapp))               ; defined in AI_UTILS.LSP
  50.    (ai_abort "DDCHPROP" nil)        ; a Nil <msg> supresses
  51. )                                    ; ai_abort's alert box dialog.
  52.  
  53. ;;; ==================== end load-time operations ===========================
  54.  
  55.  
  56. ;this is a metering prompt
  57. (defun gc_meter (gcm_pr gc_num gc_max)
  58.    (prompt 
  59.       (strcat "\r" gcm_pr " ("
  60.          (rtos(*(/(float(1+ gc_num))(float gc_max))100)2 0)
  61.          "%)"
  62.       ) ;strcat
  63.    ) ;prompt   
  64. ) ;defun
  65.  
  66. (defun C:DDSEARCH
  67.    (/ mode replace_loc _accept _replace text text_string
  68.       index pointer sset id text_string_length *olderror*
  69.    )
  70.  
  71.    ;process an ok
  72.    (defun _accept ()
  73.       (cond  
  74.          (
  75.             (or 
  76.                (= 
  77.                   (get_tile "search")
  78.                   ""
  79.                )
  80.                (= 
  81.                   (get_tile "replace")
  82.                   ""
  83.                )
  84.             )  
  85.             (set_tile "error" "Empty or invalid input")
  86.          )
  87.          (
  88.             (=
  89.                (get_tile "search")
  90.                (get_tile "replace")
  91.             )
  92.             (set_tile "error" "Search and replace are identical")
  93.          )    
  94.          (T
  95.             (setq #search_string (get_tile "search"))
  96.             (setq #replace_string (get_tile "replace"))
  97.             (setq #case_sensitive (get_tile "case"))
  98.             (setq #global (get_tile "global"))
  99.             (done_dialog 1)
  100.          )   
  101.       )  
  102.    )
  103.  
  104.    ;function for prompt to replace
  105.    (defun _replace ()
  106.       (new_dialog "ddsearch2" id "" replace_loc)
  107.       (action_tile "cancel" "(done_dialog)(exit)")
  108.       (action_tile "accept" "(setq replace_loc (done_dialog 1))")
  109.       (action_tile "skip"   "(setq replace_loc (done_dialog 0))")
  110.       (action_tile "auto"   "(done_dialog 2)")
  111.       (set_tile
  112.          "error" 
  113.          (strcat 
  114.             (rtos (1+ index) 2 0)
  115.             " of "
  116.             (rtos (sslength sset) 2 0)
  117.          )
  118.       )
  119.       (start_dialog)
  120.    )  
  121.  
  122.    ;set up the dialog identification
  123.    (setq id (load_dialog "ddsearch"))
  124.  
  125.    ;open dialog and store location as a global
  126.    (new_dialog "ddsearch" id)
  127.  
  128.    ;set those defaults
  129.    (if
  130.       #search_string
  131.       (set_tile "search" #search_string)
  132.    )
  133.    (if 
  134.       #replace_string
  135.       (set_tile "replace" #replace_string)
  136.    )
  137.    (if
  138.       #case_sensitive
  139.       (set_tile "case" #case_sensitive)
  140.    )
  141.    (if
  142.       #global
  143.       (set_tile "global" #global)
  144.    )
  145.  
  146.    ;set up callbacks
  147.    (action_tile "accept"  "(_accept)")
  148.  
  149.    ;process the look for the callbacks
  150.    (if
  151.       ;make changes if ok is picked
  152.       (=
  153.          (start_dialog)
  154.          1
  155.       )
  156.       (progn
  157.          ;get a selection set if not global
  158.          (if (/= #global "1")
  159.             (while
  160.                (=
  161.                   (setq sset (ssget (list (cons 0 "TEXT"))))
  162.                   nil
  163.                )
  164.                (prompt "\nNo entities selected")
  165.             )
  166.             (setq sset (ssget "X"(list(cons 0 "TEXT"))))
  167.          )
  168.          (setq index 0)
  169.          ;go through the selection set
  170.          (while
  171.             (/=
  172.                (setq text (ssname sset index))
  173.             nil)
  174.             ;go through each string
  175.             (setq pointer 1)
  176.             (setq text_string (cdr (assoc 1 (entget text))))
  177.             (if 
  178.                (<=
  179.                   (strlen #search_string)
  180.                   (strlen text_string)
  181.                )
  182.                (progn
  183.                   (setq text_string_length (strlen text_string))
  184.                   ;go until you reach the end of the string
  185.                   (while
  186.                      (< 
  187.                         pointer
  188.                         (+ (- text_string_length (strlen #search_string)) 2)
  189.                      )
  190.                      (if 
  191.                         (= 
  192.                            (if
  193.                               (= #case_sensitive "1")
  194.                               (substr
  195.                                  text_string
  196.                                  pointer
  197.                                  (strlen #search_string)
  198.                               )
  199.                               (strcase 
  200.                                  (substr
  201.                                     text_string 
  202.                                     pointer 
  203.                                     (strlen #search_string)
  204.                                  )
  205.                               )
  206.                            )
  207.                            (if (= #case_sensitive "1")
  208.                               #search_string
  209.                               (strcase #search_string)
  210.                            )
  211.                         )
  212.                         (progn
  213.                            (redraw text 3)
  214.                            (if (/= mode 2)
  215.                               (if (/= #global "1")
  216.                                  (setq mode (_replace))
  217.                                  (setq mode 2)
  218.                               )
  219.                            )   
  220.                            (if (> mode 0)
  221.                               (progn
  222.                                  (setq text_string
  223.                                     (if (= pointer 1)
  224.                                        (strcat
  225.                                           #replace_string
  226.                                           (substr
  227.                                              text_string
  228.                                              (+ pointer (strlen #search_string))
  229.                                           )
  230.                                        )
  231.                                        (strcat 
  232.                                           (substr 
  233.                                              text_string
  234.                                              1
  235.                                              (- pointer 1)
  236.                                           )
  237.                                           #replace_string
  238.                                           (substr
  239.                                              text_string
  240.                                              (+ pointer (strlen #search_string))
  241.                                           )
  242.                                        )
  243.                                     )
  244.                                  )
  245.                                  (entmod 
  246.                                     (subst 
  247.                                        (cons 1 text_string)
  248.                                        (assoc 1 (entget text))
  249.                                        (entget text)
  250.                                     )
  251.                                  )
  252.                                  (setq 
  253.                                     pointer 
  254.                                     (+ pointer (strlen #replace_string))
  255.                                  )
  256.                                  (setq
  257.                                     text_string_length
  258.                                     (strlen text_string)
  259.                                  )
  260.                               )
  261.                            )
  262.                            (redraw text 4)
  263.                            (setq pointer (1+ pointer))
  264.                         )
  265.                         (setq pointer (1+ pointer))
  266.                      )
  267.                   )
  268.                )
  269.             )
  270.             (gc_meter "Changing text" index (sslength sset))
  271.             (setq index (1+ index))
  272.          )
  273.       )
  274.    )
  275.    (unload_dialog id)
  276.    (setq *error* *olderror*)
  277.    (princ)
  278. ) ;defun ddsearch 
  279. (princ "DDSEARCH Loaded.")                   
  280. (princ)
  281.